package com.leochan.shuniu.api;

import org.scribe.encoders.URLHelper;
import org.scribe.http.Request;
import org.scribe.http.Response;
import org.scribe.http.Verb;
import org.scribe.oauth.Token;

import com.leochan.shuniu.Global;
import com.leochan.shuniu.ThreadType;
import com.leochan.utils.DebugUtils;

public class MessageAPI extends BaseAPI {

	private static final String threads_str = "http://api.shuniu.net/api/v1/threads/%d.json%s";
	private static final String messages_str = "http://api.shuniu.net/api/v1/messages.json%s";
	private static final String messages_send_str = "http://api.shuniu.net/api/v1/messages/send.json%s";
	private static final String messages_private_str = "http://api.shuniu.net/api/v1/messages/private.json%s";
	private static final String messages_received_str = "http://api.shuniu.net/api/v1/messages/received.json%s";
	private static final String messages_following_str = "http://api.shuniu.net/api/v1/messages/following.json%s";
	private static final String messages_from_user_str = "http://api.shuniu.net/api/v1/messages/from_user.json%s";
	private static final String messages_in_thread_str = "http://api.shuniu.net/api/v1/messages/in_thread.json%s";

	private static final String Threaded = "threaded";

	// GET http://api.shuniu.net/api/v1/threads/[:id].json
	public static void threads(Token token, long id, long network_id,
			long group_id, long newer_than, long older_than, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, type);
		String format = String.format(threads_str, id,
				URLHelper.queryString(params));
		DebugUtils.printLogD(format);
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	public static void messages_GET(Token token) {
		DebugUtils.printLogD();
		messages(token, Verb.GET);
	}

	public static void messages_POST(Token token) {
		DebugUtils.printLogD();
		messages(token, Verb.POST);
	}

	// GET/POST http://api.shuniu.net/api/v1/messages.json
	private static void messages(Token token, Verb verb) {
		initParams(token);
		String format = String.format(messages_str,
				URLHelper.queryString(params));
		Request req = new Request(verb, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	// GET http://api.shuniu.net/api/v1/messages/send.json
	public static void messages_send(Token token, long network_id,
			long newer_than, long older_than, int limit, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, ThreadType.False);
		String format = String.format(messages_send_str,
				URLHelper.queryString(params));
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	// GET http://api.shuniu.net/api/v1/messages/private.json
	public static void messages_private(Token token, long network_id,
			long newer_than, long older_than, int limit, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, type);
		String format = String.format(messages_private_str,
				URLHelper.queryString(params));
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	// GET http://api.shuniu.net/api/v1/messages/received.json
	public static void messages_received(Token token, long network_id,
			long newer_than, long older_than, int limit, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, type);
		String format = String.format(messages_received_str,
				URLHelper.queryString(params));
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	// GET http://api.shuniu.net/api/v1/messages/following.json
	public static void messages_following(Token token, long network_id,
			long newer_than, long older_than, int limit, int id, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, type);
		String format = String.format(messages_following_str,
				URLHelper.queryString(params));
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	// GET http://api.shuniu.net/api/v1/messages/from_user/[:id].json
	public static void messages_from_user(Token token, long network_id,
			long newer_than, long older_than, int limit, int id, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, type);
		String format = String.format(messages_from_user_str,
				URLHelper.queryString(params));
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

	// GET http://api.shuniu.net/api/v1/messages/in_thread/[:id].json
	public static void messages_in_thread(Token token, long network_id,
			long newer_than, long older_than, int limit, int id, ThreadType type) {
		DebugUtils.printLogD();
		initParams(token);
		params.put(Threaded, type);
		String format = String.format(messages_in_thread_str,
				URLHelper.queryString(params));
		Request req = new Request(Verb.GET, format);
		Response resp = req.send(Global.proxy);
		DebugUtils.printLogD(resp.getBody());
		DebugUtils.printLogD("====================================");
	}

}
